package com.daocao.jy.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.daocao.jy.R;
import com.daocao.jy.api.Api;
import com.daocao.jy.api.bean.BirthDayBean;
import com.daocao.jy.api.bean.ModifySendBean;
import com.daocao.jy.api.bean.NicknameBean;
import com.daocao.jy.api.bean.UserSexBean;
import com.daocao.jy.base.BaseActivity;
import com.daocao.jy.constant.UserInfoGlobal;
import com.daocao.jy.util.Network;
import com.daocao.jy.util.PreferenceUtils;
import com.daocao.jy.util.RxLifeUtil;
import com.daocao.jy.util.ToastUtil;
import com.daocao.jy.view.V19FrameLayout;


import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class PersonalCenterActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.btn_icon)
    LinearLayout btnIcon;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.btn_phone)
    LinearLayout btnPhone;
    @BindView(R.id.txt_nickname)
    TextView txtNickname;
    @BindView(R.id.btn_nickname)
    LinearLayout btnNickname;
    @BindView(R.id.txt_sex)
    TextView txtSex;
    @BindView(R.id.btn_sex)
    LinearLayout btnSex;
    @BindView(R.id.txt_birth)
    TextView txtBirth;
    @BindView(R.id.btn_birth)
    LinearLayout btnBirth;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    private Unbinder unbiner;
    private TimePickerView pvTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("个人资料");
        initTimePicker();
        txtPhone.setText(UserInfoGlobal.getUserPhone());
        txtBirth.setText(UserInfoGlobal.getUserBirth());
        txtNickname.setText(UserInfoGlobal.getUserNick());
        txtSex.setText(UserInfoGlobal.getUserSex());

    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
//                Toast.makeText(PersonalCenterActivity.this, getTime(date), Toast.LENGTH_SHORT).show();
                txtBirth.setText(getTime(date));
                getBirthday(String.valueOf(date.getTime() / 1000));
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {

                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }

    public void getNickname(String nickname) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        NicknameBean item = new NicknameBean();
        item.nickname = nickname;

        loadingDialog.show();
        Api.api_service.getNickname(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.saveNickName(nickname);
                    ToastUtil.makeText(this, "修改成功");
                });
    }

    public void getUserSex(String sex) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        UserSexBean item = new UserSexBean();
        item.sex = sex;

        loadingDialog.show();
        Api.api_service.getUserSex(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    txtSex.setText(sex.equals("1") ? "男" : "女");
                    UserInfoGlobal.saveUserSex(sex);
                    ToastUtil.makeText(this, "修改成功");
                });
    }

    public void getBirthday(String birthday) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        BirthDayBean item = new BirthDayBean();
        item.birthday = birthday;

        loadingDialog.show();
        Api.api_service.getBirthday(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_icon, R.id.btn_phone, R.id.btn_sex, R.id.btn_birth, R.id.btn_submit, R.id.txt_nickname})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;

            case R.id.txt_nickname:
                com.daocao.jy.util.Dialog.showDefaultEditDialog(this, "修改昵称", "请输入昵称", "取消", "确定", new com.daocao.jy.util.Dialog.DialogEditClickListener() {
                    @Override
                    public void confirm(String content) {
                        getNickname(content);
                    }

                    @Override
                    public void cancel() {

                    }
                });
                break;
            case R.id.btn_icon:
                break;
            case R.id.btn_phone:
                break;
            case R.id.btn_sex:
                com.daocao.jy.util.Dialog.showDefaultDialog(this, "选择性别", "", "男", "女", false, new com.daocao.jy.util.Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        getUserSex("2");
                    }

                    @Override
                    public void cancel() {
                        getUserSex("1");
                    }
                });
                break;
            case R.id.btn_birth:
                pvTime.show();
                break;
            case R.id.btn_submit:
                break;
        }
    }
}
